/**
 * 
 */
package com.SoapClasses;


import java.io.IOException;
import java.net.SocketTimeoutException;
import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
import android.util.Log;
import com.Data.Options;
import com.Managers.LogManagerClass;


/**
 * @author Chris
 * 
 */
public class SoapOptions {

    private static String TAG = "SOAP OPTIONS";

    SoapSerializationEnvelope envelope;

    PropertyInfo pi;

    SoapObject Request;

    public Options getOptions(int id) {

        String METHOD_NAME = "getOptions";
        String SOAP_ACTION = "http://ShareUp.com/getOptions";
        //
        Request = new SoapObject(SoapUtil.NAMESPACE, METHOD_NAME);

        Request.addProperty("id", id);
        //
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(Request);
        HttpTransportSE httpTransport = new HttpTransportSE(SoapUtil.URL, SoapUtil.TIMEOUT);
        //
        Object response = null;
        try {
            httpTransport.call(SOAP_ACTION, envelope);
            response = envelope.getResponse();

            // IF EVERYTHING WENT OKAY IT GOING TO RETURN -1
            return getOptionsData(response);
        }
        catch (SocketTimeoutException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            Options opt = new Options(null);
            opt.setIdUser(-10);
            return opt;
        }
        catch (SoapFault e) {
            StackTraceElement[] stack = e.getStackTrace();
            String x = " ";
            for (StackTraceElement line : stack) {
                x += line.toString();
            }
            Log.e(TAG + ": ", x);
            LogManagerClass.writeLog(TAG + ": " + x);
            return null;
        }
        catch (IOException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            Options opt = new Options(null);
            opt.setIdUser(-10);
            return opt;
        }
        catch (XmlPullParserException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            Options opt = new Options(null);
            opt.setIdUser(-11);
            return opt;
        }

    }

    public String updateOptions(String[] data) {

        String METHOD_NAME = "saveOptions";
        String SOAP_ACTION = "http://ShareUp.com/saveOptions";
        //
        Request = new SoapObject(SoapUtil.NAMESPACE, METHOD_NAME);
        //
        String info = "";
        for (int i = 0; i < data.length; i++) {
            info += data[i] + "@";
        }
        Request.addProperty("data", info);
        //
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(Request);
        HttpTransportSE httpTransport = new HttpTransportSE(SoapUtil.URL, SoapUtil.TIMEOUT);
        //
        Object response = null;
        try {
            httpTransport.call(SOAP_ACTION, envelope);
            response = envelope.getResponse();
        }
        catch (SocketTimeoutException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            return "-10";
        }
        catch (SoapFault e) {
            StackTraceElement[] stack = e.getStackTrace();
            String x = " ";
            for (StackTraceElement line : stack) {
                x += line.toString();
            }
            Log.e(TAG + ": ", x);
            LogManagerClass.writeLog(TAG + ": " + x);
        }
        catch (IOException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            return x.getMessage();
        }
        catch (XmlPullParserException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            return x.getMessage();
        }
        // IF EVERYTHING WENT OKAY IT GOING TO RETURN -1
        return response.toString();
    }

    private Options getOptionsData(Object sp) {

        String RES = sp.toString();

        try {
            JSONObject jo = new JSONObject(RES);
            Options opt = new Options();
            opt.setAllowFollow(jo.getInt("AllowFollow"));
            opt.setAllowShare(jo.getInt("AllowShare"));
            opt.setBlurCover(jo.getInt("BlurCover"));

            return opt;

        }
        catch (JSONException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());

            return null;
        }
    }
}
